class CreateEventTables < ActiveRecord::Migration

  def self.up
    begin sport_up() rescue puts "Creation failed: " + $! end
    begin rounds_up() rescue puts "Creation failed: " + $! end
    begin intervals_up() rescue puts "Creation failed: " + $! end
    begin games_up() rescue puts "Creation failed: " + $! end
    begin events_up() rescue puts "Creation failed: " + $! end
    begin scores_up() rescue puts "Creation failed: " + $! end
    begin honors_up() rescue puts "Creation failed: " + $! end
  end

  def self.down
    begin drop_table :sport        rescue puts "Failed to drop: " + $! end

    begin drop_table :rounds        rescue puts "Failed to drop: " + $! end
    begin drop_table :rounds_scores rescue puts "Failed to drop: " + $! end

    begin drop_table :scores      rescue puts "Failed to drop: " + $! end
    begin drop_table :honors  rescue puts "Failed to drop: " + $! end

    begin drop_table :intervals          rescue puts "Failed to drop: " + $! end
    begin drop_table :intervals_scores   rescue puts "Failed to drop: " + $! end
    begin drop_table :intervals_rounds   rescue puts "Failed to drop: " + $! end

    begin drop_table :games       rescue puts "Failed to drop: " + $! end
    begin drop_table :games_tags       rescue puts "Failed to drop: " + $! end
    begin drop_table :games_intervals  rescue puts "Failed to drop: " + $! end
    begin drop_table :games_rule_sets  rescue puts "Failed to drop: " + $! end
    begin drop_table :games_links      rescue puts "Failed to drop: " + $! end
    begin drop_table :games_images     rescue puts "Failed to drop: " + $! end
    begin drop_table :games_scores     rescue puts "Failed to drop: " + $! end
    begin drop_table :games_teams     rescue puts "Failed to drop: " + $! end

    begin drop_table :events      rescue puts "Failed to drop: " + $! end
    begin drop_table :events_games   rescue puts "Failed to drop: " + $! end
    begin drop_table :events_images  rescue puts "Failed to drop: " + $! end
    begin drop_table :events_tags    rescue puts "Failed to drop: " + $! end
  end

  def self.sport_up
    #Define a set of sensible defaults for the particular sport we are associated with.
    create_table :sport do |t|
      t.string :name
      t.string :db_name
      t.string :host_url
      t.string :url_path
      t.string :game_name
      t.string :season_name
      t.string :event_name
      t.string :league_name
      t.string :interval_name
      t.string :round_name
      t.string :player_name
      t.string :interval_time
      t.string :round_time
      t.string :score_name
      t.timestamps
    end
  end


  def self.games_up
    create_table :games do |t|
      t.string :name
      t.timestamps
    end

    create_table :games_tags, :id => false do |t|
      t.integer :game_id
      t.integer :tag_id
    end

    create_table :games_intervals, :id => false do |t|
      t.integer :game_id
      t.integer :interval_id
    end

    create_table :games_rule_sets, :id => false do |t|
      t.integer :game_id
      t.integer :rule_set_id
    end

    create_table :games_links, :id => false do |t|
      t.integer :game_id
      t.integer :link_id
    end

    create_table :games_images, :id => false do |t|
      t.integer :game_id
      t.integer :image_id
    end

    create_table :games_scores, :id => false do |t|
      t.integer :game_id
      t.integer :score_id
    end

    create_table :games_teams, :id => false do |t|
      t.integer :game_id
      t.integer :team_id
    end
  end

  def self.events_up
    create_table :events do |t|
      t.string :name
      t.datetime :start_date
      t.datetime :end_date
      t.timestamps
    end

    create_table :events_games, :id => false do |t|
      t.integer :event_id
      t.integer :game_id
    end

    create_table :events_images, :id => false do |t|
      t.integer :event_id
      t.integer :image_id
    end

    create_table :events_tags, :id => false do |t|
      t.integer :tag_id
      t.integer :event_id
    end
  end

  def self.intervals_up
    create_table :intervals do |t|
      t.string   :name
      t.datetime :start_time
      t.datetime :end_time
      t.timestamps
    end

    create_table :intervals_scores, :id => false do |t|
      t.integer :score_id
      t.integer :interval_id
    end

    create_table :intervals_rounds, :id => false do |t|
      t.integer :round_id
      t.integer :interval_id
    end
  end

  def self.scores_up
    create_table :scores do |t|
      t.integer    :points      #Number of points
      t.string     :identifier  #Friendly name (Team name, Individual's handle etc) 
      t.string     :score_type  #Team, individual etc
      t.timestamps
    end
  end

  def self.rounds_up
    create_table :rounds do |t|
      t.integer :round_counter, :default => 0
      t.string  :notes, :default => nil
      t.timestamps
    end
   
    create_table :rounds_scores, :id => false do |t|
      t.integer :round_id
      t.integer :score_id
      end
  end

  def self.honors_up
    create_table :honors do |t|

      t.timestamps
    end
  end
end
